#include "BallGenerator.h"

BallGenerator* BallGenerator::instance = 0;

BallGenerator::BallGenerator(Stage *stage) {
	this->stage = stage;
}

BallGenerator::BallGenerator() {
}

BallGenerator* BallGenerator::getInstance(){
	if (!instance){
		instance = new BallGenerator();
	}
	return instance;
}

BallGenerator* BallGenerator::getInstance(Stage* stage){
	if (!instance){
		instance = new BallGenerator(stage);
	}
	return instance;
}

Ball* BallGenerator::getBall(Stage* stage){
	int ballTypesNumber = stage->getBallTypes().size();
	int choosenBallTypeNumber =(rand()%(ballTypesNumber-0))+0;
	int index = 0;

	list<ballType*>::iterator theIterator;
	list<ballType*> ballsType = stage->getBallTypes();

	
	for(theIterator = ballsType.begin(); (theIterator != ballsType.end()) && (index < choosenBallTypeNumber); theIterator++){
		index++;
	}
	
	ballType *choosenBallType = *theIterator;
	Ball* ball= new Ball(choosenBallType->getColor(),choosenBallType->getLineColor(),stage->getVelocity());
	if(choosenBallType->getTexture()!=NULL){
		ball->setTexture(choosenBallType->getTexture());
	}
	ball->setRadius(choosenBallType->getRadious());
	ball->setSurface(choosenBallType->getSurface());
	ball->setPosition(stage->getSource()->getPosition());
	ball->setReverseModeOff();
	ball->setStageReversePowerOff();
	ball->start();
	ball->setNeedCheck(false);
	return ball;
}

Ball* BallGenerator::getBall(){
	if (stage != NULL)
		return getBall(stage);
	return NULL;
}

void BallGenerator::setStage(Stage *stage){
	this->stage = stage;
}

BallGenerator::~BallGenerator() {
}